﻿using System.Text;
using System.Windows.Forms;

namespace MyControls
{
    public partial class ExpCom : UserControl
    {
        #region Properties

        private string _pt = string.Empty;
        /// <summary>
        /// Thứ tự của phần tử để hiển thị ra ngoài
        /// </summary>
        public string PT
        {
            set
            {
                _pt = value;
                lblIndex.Text = _pt;
            }
            get { return _pt; }
        }

        private int _level = 0;
        /// <summary>
        /// Level của phần tử: 0 ~ dễ; 1: trung bình; 2: khó
        /// </summary>
        public int Level
        {
            set { _level = value; }
            get { return _level; }
        }

        private string _expression = string.Empty;
        /// <summary>
        /// Biểu thức
        /// </summary>
        public string Expression
        {
            set { _expression = value; }
            get { return _expression; }
        }

        private string _expression1 = string.Empty;
        /// <summary>
        /// Biểu thức
        /// </summary>
        public string Expression1
        {
            set { _expression1 = value; }
            get { return _expression1; }
        }

        private string _answer = string.Empty;
        /// <summary>
        /// Đáp án
        /// </summary>
        public string Answer
        {
            set { _answer = value; }
            get { return _answer; }
        }

        #endregion

        public ExpCom()
        {
            InitializeComponent();
        }

        public ExpCom(string Exp, string Exp1, string answer, int level)
        {
            InitializeComponent();
            Expression = Exp;
            Expression1 = Exp1;
            Answer = answer;
            string tmp = Expression;
            string [] _words = answer.Split(' ');
            for (int i = 0; i < _words.Length; i++)
            {
                int idx = tmp.IndexOf('?');
                if (idx > -1)
                {
                    string xxx = tmp.Substring(0, idx);
                    xxx += _words[i] + tmp.Substring(idx + 1, tmp.Length - (idx + 1));
                    tmp = xxx;
                }
            }
            txtExpression.Text = tmp;
            Level = level;
        }

        private string Format(string input, bool isExp)
        {
            string _output = input.Replace(" ", string.Empty);
            if(isExp)
            {
                _output = _output.Replace("0", string.Empty);
                _output = _output.Replace("1", string.Empty);
                _output = _output.Replace("2", string.Empty);
                _output = _output.Replace("3", string.Empty);
                _output = _output.Replace("4", string.Empty);
                _output = _output.Replace("5", string.Empty);
                _output = _output.Replace("6", string.Empty);
                _output = _output.Replace("7", string.Empty);
                _output = _output.Replace("8", string.Empty);
                _output = _output.Replace("9", string.Empty);
                _output = _output.Replace(" ", string.Empty);
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < _output.Length; i++)
                {
                    if (stringBuilder.Length == 0 && _output[0] != '(')
                        stringBuilder.Append("?");
                    stringBuilder.Append(_output[i]+"?");
                }
                _output = stringBuilder.ToString();
            }
            else
            {
                _output =
                    _output.Replace('+', ' ').Replace('-', ' ').Replace('x', ' ').Replace(':', ' ').Replace('(', ' ').
                        Replace(')', ' ').Replace('=', ' ');
                _output = _output.Trim();
                _output = _output.Replace("  ", " ");
            }
            return _output;
        }

        private string FormatExp(string input)
        {
            string _output = input.Replace(" ", string.Empty);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < _output.Length; i++)
            {
                if (stringBuilder.Length > 0)
                    stringBuilder.Append(' ');
                stringBuilder.Append(_output[i]);
            }
            _output = stringBuilder.ToString();
            return _output;
        }

        public void GetData()
        {
            string tmp = txtExpression.Text;
            Expression = FormatExp(Format(tmp, true));
            Expression1 = Expression.Replace('x', '*').Replace(':', '/');
            Answer = Format(tmp, false);
        }
    }
}
